<?php
// 定义存储秘密的文件路径
$secretFile = 'secrets.txt';

// 处理用户提交的秘密
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['secret'])) {
    $newSecret = $_POST['secret'];
    // 打开文件以追加模式写入
    if ($file = fopen($secretFile, 'a')) {
        fwrite($file, $newSecret . PHP_EOL);
        fclose($file);
        $successMessage = "你的秘密已成功分享到树洞！";
    } else {
        $errorMessage = "抱歉，无法保存你的秘密，请稍后再试。";
    }
}

// 读取所有秘密
$secrets = [];
if (file_exists($secretFile)) {
    $secrets = file($secretFile, FILE_IGNORE_NEW_LINES);
    // 打乱秘密顺序，模拟随机查看
    shuffle($secrets);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>秘密树洞网页游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        textarea {
            width: 100%;
            height: 100px;
            margin-bottom: 10px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }

        .secret-list {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }

        .secret {
            margin-bottom: 10px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
    </style>
</head>

<body>
    <h1>秘密树洞网页游戏</h1>

    <?php if (isset($successMessage)): ?>
        <div class="message success"><?php echo $successMessage; ?></div>
    <?php endif; ?>

    <?php if (isset($errorMessage)): ?>
        <div class="message error"><?php echo $errorMessage; ?></div>
    <?php endif; ?>

    <form method="post">
        <label for="secret">分享你的秘密:</label><br>
        <textarea id="secret" name="secret" required></textarea><br>
        <button type="submit">分享秘密</button>
    </form>

    <div class="secret-list">
        <h2>看看别人的秘密</h2>
        <?php if (empty($secrets)): ?>
            <p>目前树洞里还没有秘密，快来分享你的吧！</p>
        <?php else: ?>
            <?php foreach ($secrets as $secret): ?>
                <div class="secret">
                    <p><?php echo htmlspecialchars($secret); ?></p>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
</body>

</html>